import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class VideoPlayerScreen extends StatefulWidget {
  final String videoUrl;

  VideoPlayerScreen({Key? key, required this.videoUrl}) : super(key: key);

  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  late VideoPlayerController _videoController;
  ChewieController? _chewieController;
  late String videoTitle;

  @override
  void initState() {
    super.initState();

    _videoController = VideoPlayerController.file(File(widget.videoUrl))
      ..initialize().then((_) {
        setState(() {
          _chewieController = ChewieController(
            videoPlayerController: _videoController,
            autoPlay: true,
            looping: false,
            // Ensure the controls are visible
            showControls: true,
            // Customizing the material progress bar
            materialProgressColors: ChewieProgressColors(
              playedColor: Colors.white,
              handleColor: Colors.white,
              backgroundColor: Colors.grey.shade50,
              bufferedColor: Colors.grey,
            ),
            placeholder: Container(
              color: Colors.black87,
            ),
            autoInitialize: true,
          );
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: [

            Column(
              children: [
                Expanded(
                  child: Center(
                    child: _chewieController != null && _chewieController!.videoPlayerController.value.isInitialized
                        ? Chewie(controller: _chewieController!)
                        : CircularProgressIndicator(),
                  ),
                ),
              ],
            ),

            CustomControls(widget.videoUrl.split('/').last),

          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _videoController.dispose();
    _chewieController?.dispose();
    super.dispose();
  }
}

// CustomControls remains the same
class CustomControls extends StatelessWidget {
  final String title;

  CustomControls(this.title);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          left: 10,
          top: 0,
          child: IconButton(
            icon: Icon(Icons.arrow_back, color: Colors.white),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        Positioned(
          top: 12,
          left: 0,
          right: 0,
          child: Text(
            title,
            style: TextStyle(color: Colors.white, fontSize: 15),
            textAlign: TextAlign.center,
          ),
        ),
      ],
    );
  }
}
